//
//  MovieIntroducedViewController.m
//  豆瓣
//
//  Created by aaaaaa on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieIntroducedViewController.h"
#import "MovieListModel.h"
#import "Tools.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"

@interface MovieIntroducedViewController ()

@property (nonatomic, retain) MovieModel *movieModel;

@end

@implementation MovieIntroducedViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    [self handleData];
    [self createScrollerView];
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.movieListModel.movieName;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.rightBarButtonItem = RIGHTBAR;
    self.navigationItem.leftBarButtonItem = LEFTBAR;
}

- (void)leftBarAction:(UIBarButtonItem *)bar{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)rightBarAction:(UIBarButtonItem *)bar{
    
}


- (void)handleData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"m%@", self.movieListModel.movieId] ofType:@"txt"];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:0 error:&error];
    NSDictionary *movieDic = [dictionary objectForKey:@"result"];
    
    self.movieModel = [[MovieModel alloc] init];
    [self.movieModel setValuesForKeysWithDictionary:movieDic];
    
}


- (void)createScrollerView{

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 64, self.view.bounds.size.width - 40, self.view.bounds.size.height - 64 - 50)];
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    
    //图片
    CGFloat imageHeight = [Tools getImageHeight:self.movieModel.poster withWidth:scrollView.bounds.size.width * 0.3];
    UIImageView *movieImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, scrollView.bounds.size.width * 0.3, imageHeight)];
    [movieImageView sd_setImageWithURL:[NSURL URLWithString:self.movieModel.poster]];
    [scrollView addSubview:movieImageView];
    
    
    
    //信息
    NSString *ratingStr = [NSString stringWithFormat:@"评分：%@   （%ld评论）", self.movieModel.rating, self.movieModel.rating_count];
    
    NSArray *strArray = @[ratingStr, self.movieModel.release_date, self.movieModel.runtime, self.movieModel.genres, self.movieModel.film_locations];
    
    CGFloat lableHeight = imageHeight / 5;
    for (int i = 0; i < 5; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * 0.3 + 50 , lableHeight * i + 20, scrollView.bounds.size.width * 0.7 - 40, lableHeight)];
        lable.font = FONT;
        lable.text = strArray[i];
        [scrollView addSubview:lable];
        [lable release];
    }
    
    
    //制作人标题
    UILabel *actorLable = [[UILabel alloc] initWithFrame:CGRectMake(0, imageHeight + 20 + 20, scrollView.bounds.size.width, 30)];
    actorLable.text = @"制作人";
    actorLable.font = [UIFont systemFontOfSize:24];
    [scrollView addSubview:actorLable];
    
    //制作人内容
    CGFloat actorsHeight = [Tools getTextHeight:self.movieModel.actors withWidth:scrollView.bounds.size.width withFontSize:16];
    UILabel *actorsLable = [[UILabel alloc] initWithFrame:CGRectMake(0, imageHeight + 20 + 20 + 30 + 10, scrollView.bounds.size.width, actorsHeight)];
    actorsLable.font = FONT;
    actorsLable.text = self.movieModel.actors;
    actorsLable.numberOfLines = 0;
    [scrollView addSubview:actorsLable];
    
    //电影情节标题
    UILabel *plotTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, imageHeight + 20 + 20 + 30 + 10 + actorsHeight + 20, scrollView.bounds.size.width, 30)];
    plotTitleLable.text = @"电影情节";
    plotTitleLable.font = [UIFont systemFontOfSize:24];
    [scrollView addSubview:plotTitleLable];
    
    //电影情节内容
    CGFloat plotHeight = [Tools getTextHeight:self.movieModel.plot_simple withWidth:scrollView.bounds.size.width withFontSize:16];
    UILabel *plotContentLable = [[UILabel alloc] initWithFrame:CGRectMake(0, imageHeight + 20 + 20 + 30 + 10 + actorsHeight + 20 + 30 + 10, scrollView.bounds.size.width, plotHeight)];
    plotContentLable.font = FONT;
    plotContentLable.text = self.movieModel.plot_simple;
    plotContentLable.numberOfLines = 0;
    [scrollView addSubview:plotContentLable];
    
    scrollView.contentSize = CGSizeMake(0, imageHeight + 20 + 20 + 30 + 10 + actorsHeight + 20 + 30 + 10 + plotHeight);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
